#include "helper.h"
#include "definitions.h"

#ifndef INCLUDE_MATPLOTLIB_CPP
#define INCLUDE_MATPLOTLIB_CPP
#include <matplotlibcpp.h>
#endif // INCLUDE_MATPLOTLIB_CPP

#include <string>
#include <math.h>

namespace plt = matplotlibcpp;

double SPLINTER::operator<<(SPLINTER::Function &spline, double x)
{
    SPLINTER::DenseVector xs(1);
    xs(0) = x;
    return spline.eval(xs);
}

void SPLINTER::plotSpline(const std::vector<double> &x, const std::vector<double> &y, const std::map<std::string, std::string> &keywords)
{
    plt::plot(x, y, keywords);
    plt::legend();
}

void SPLINTER::plotSpline(SPLINTER::Function &spline, double start, double end, const std::map<std::string, std::string> &keywords, int points)
{
    std::vector<double> x(points), y(points);

    for (int i = 0; i < points; i++)
    {
        x[i] = start + (end - start) * i / (points - 1);
        y[i] = spline << x[i];
    }
    plt::plot(x, y, keywords);
    plt::legend();
}

void SPLINTER::plotSplineTheta(Function &spline_x, Function &spline_y, SPLINTER::ISampler &sampler, const std::map<std::string, std::string> &keywords, int points)
{
    std::vector<double> x(points), y(points);
    double start = -M_PI / 2, end = M_PI * 3 / 2;
    for (int i = 0; i < points; i++)
    {
        SPLINTER::DenseVector theta(1);
        theta(0) = start + (end - start) * i / (points - 1);
        x[i] = spline_x << theta(0);
        y[i] = spline_y << theta(0);
    }
    plt::plot(x, y, keywords);
    plt::legend();
}

void SPLINTER::subplot(long nrows, long ncols, long plot_number)
{
    plt::subplot(nrows, ncols, plot_number);
}

void SPLINTER::show()
{
    plt::show();
}

SPLINTER::DenseVector SPLINTER::doubleToDenseVector(double x)
{
    SPLINTER::DenseVector X(1);
    X(0) = x;
    return X;
}